/**
 * Title: jFamilyTree File System provider
 * Description: jFamilyTree File System provider
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright: (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.provider.fs;

import java.awt.Color;
import java.awt.Image;
import java.io.File;
import java.util.Date;
import java.util.Random;

import javax.swing.ImageIcon;

import jFamilyTree.model.IEdge;
import jFamilyTree.model.INode;
import jFamilyTree.model.MutableNode;

/**
 * FsNode implementation for file system provider
 * 
 * @author Bernard Bou
 */
public class FsNode extends MutableNode implements INode
{
	/**
	 * Randomize used to generate ids
	 */
	static private Random theRandomizer = new Random();

	/**
	 * Make node id
	 * 
	 * @return node id
	 */
	static public String makeNodeId()
	{
		return "X" + Long.toHexString(theRandomizer.nextLong());
	}

	// S T A T I C . D A T A

	/**
	 * File background color
	 */
	public static final Color theDefaultFileBackColor = new Color(0xffff80);

	/**
	 * File foreground color
	 */
	public static final Color theDefaultFileForeColor = Color.BLACK;

	/**
	 * File image
	 */
	private static final Image theDefaultFileImage = new ImageIcon(FsNode.class.getResource("images/file.gif")).getImage();

	/**
	 * Folder background color
	 */
	private static final Color theDefaultFolderBackColor = Color.ORANGE;

	/**
	 * Folder foreground color
	 */
	private static final Color theDefaultFolderForeColor = Color.BLACK;

	/**
	 * Folder image
	 */
	private static final Image theDefaultFolderImage = new ImageIcon(FsNode.class.getResource("images/folder.gif")).getImage();

	// C O N S T R U C T O R

	/**
	 * Constructor
	 * 
	 * @param thisParent
	 *        parent
	 * @param thisFile
	 *        file
	 */
	public FsNode(INode thisParent, File thisFile)
	{
		super(thisParent, makeNodeId());

		setLabel(thisFile.getName());
		setLink(thisFile.toURI().toString());
		setContent(thisFile.length() + "\n" + new Date(thisFile.lastModified()));

		if (thisFile.isDirectory())
		{
			setBackColor(theDefaultFolderBackColor);
			setForeColor(theDefaultFolderForeColor);
			setEdgeStyle(IEdge.SOLID | IEdge.TOTRIANGLE | IEdge.TOSOLID);
			setEdgeColor(Color.BLACK);
			setImage(theDefaultFolderImage);

			// recurse
			String[] theSubFiles = thisFile.list();
			for (int i = 0; i < theSubFiles.length; i++)
			{
				File thisChild = new File(thisFile.getPath() + File.separator + theSubFiles[i]);
				new FsNode(this, thisChild);
			}
		}
		else
		{
			setImage(theDefaultFileImage);
		}
	}
}
